package cn.nawang.ebeim.client.version;

import java.util.Arrays;

/**
 * Given a non-negative number represented as an array of digits, plus one to the number.
 * The digits are stored such that the most significant digit is at the head of the list.
 * <p>
 * Created by GanJc on 2016/2/24.
 */
public class PlusOne {

    public static void main(String[] args) {
        plusOne(new int[]{1, 2});
        plusOne(new int[]{9});
        plusOne(new int[]{9, 9});
        plusOne(new int[]{1, 2, 9});
    }

    public static void plusOne(int[] a) {
        int i = a.length - 1;
        for (; ; ) {
            if (i == 0) {
                int[] b = new int[a.length + 1];
                b[0] = 1;
                a[i] = 0;
                System.arraycopy(a, 0, b, 1, a.length);
                a = b;
                break;
            }
            a[i]++;
            int last = a[i];
            if (last < 9) {
                a[i] = last;
                break;
            } else {
                a[i] = 0;
                i--;
            }
        }
        System.out.println(Arrays.toString(a));
    }
}
